package com.aai.servlets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aai.db.MySQLConnect;

public class ArticlesResultSetServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7090914564169592198L;

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		MySQLConnect sql = MySQLConnect.getInstance();
		Connection connec = sql.connect();
		if (connec != null) {
			String query = "SELECT * FROM news ORDER BY date DESC";
			Statement st_news;
			int i_news = 0;
			ResultSet result = null;

			try {
				st_news = connec.createStatement();
				result = st_news.executeQuery(query);
				result.last();
				i_news = result.getRow();
				result.first();

			} catch (SQLException e) {
				e.printStackTrace();
			}

			if (i_news == 0) {
				RequestDispatcher dispatch = request
						.getRequestDispatcher("/articles.jsp?message=noarticle");
				dispatch.forward(request, response);
			} else {

				request.setAttribute("articlesResultSet", (ResultSet) result);
				RequestDispatcher dispatch = request
						.getRequestDispatcher("/articles.jsp?message=ok");
				dispatch.forward(request, response);

			}
		}
	}
}
